package com.model.common.utils.image;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.model.common.R;
import com.model.common.base.basic.BaseApplication;

/**
 * Create by CARSON on 2021/12/24.
 * 图片加载器
 */
public class GlideUtils {

    //圆形图片
    public static void circleImg(final ImageView imageView, int placeholder, String path) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholder)
                .error(placeholder);
        Glide.with(BaseApplication.context).asBitmap().load(path).apply(options).into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(BaseApplication.context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    public static void circleImg(final ImageView imageView, String path) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.icon_default)
                .error(R.drawable.icon_default);
        Glide.with(BaseApplication.context).asBitmap().load(path).apply(options).into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(BaseApplication.context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    //圆形图片
    public static void circleImg(final ImageView imageView, int resource) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.icon_default)
                .error(R.drawable.icon_default);
        Glide.with(BaseApplication.context).asBitmap().load(resource).apply(options).into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(BaseApplication.context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    /*
     * 带默认图
     * */
    public static void showImg(ImageView imageView, int defaultImage, String path) {

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(defaultImage)
                .error(defaultImage);
        if (TextUtils.isEmpty(path)) {//没有图片，显示默认图片
            Glide.with(BaseApplication.context).load(defaultImage).apply(options).into(imageView);
            return;
        }
        Glide.with(BaseApplication.context).load(path).apply(options).into(imageView);
    }

    public static void showImg(ImageView imageView, String path) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.icon_default)
                .error(R.drawable.icon_default);
        Glide.with(BaseApplication.context).load(path).apply(options).into(imageView);
    }

    public static void showImg(ImageView imageView, int resource) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.icon_default)
                .error(R.drawable.icon_default);
        Glide.with(BaseApplication.context).load(resource).apply(options).into(imageView);
    }

    /*正方形图片15圆角*/
    public static void squareImg(final ImageView imageView, int placeholder, String path) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholder)
                .error(placeholder);
        Glide.with(BaseApplication.context).asBitmap().load(path).apply(options).into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(BaseApplication.context.getResources(), resource);
                circularBitmapDrawable.setCornerRadius(15);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }
}
